package com.erpanderp.wam.usm.model.service.leave;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;


import com.erpanderp.wam.usm.model.entity.leave.LeaveBalance;
import com.erpanderp.wam.usm.model.entity.leave.LeaveType;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.HibernateUtil;

public class LeaveTypeDAO {
	
	public LeaveType createLeaveType(LeaveType leaveType){
		try{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		System.out.println("FLUSH MODE: " + session.getFlushMode());
		session.setFlushMode(FlushMode.COMMIT);
		session.save(leaveType);
		session.getTransaction().commit();
		return leaveType;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
		
		
	}
	
	public boolean updateLeaveType(LeaveType leaveType){
		boolean isLeaveTypeUpdated = false;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			session.update(leaveType);
			session.getTransaction().commit();
			isLeaveTypeUpdated = true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return isLeaveTypeUpdated;
		
	}
	
	public List<LeaveType> getLeaveTypes(){
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString="from LeaveType l";
			Query query = session.createQuery(queryString).setCacheable(false);
			return (List<LeaveType>)query.list();			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public LeaveType getLeaveTypeById(Long leaveTypeid)
	{   
		System.out.println("LEAVE TYPE IS IN DAO ="+leaveTypeid);
		LeaveType leaveType = null;
		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			String queryString = "from LeaveType l where l.leaveTypeid=:leaveTypeid";
			Query query = session.createQuery(queryString).setLong("leaveTypeid", leaveTypeid);
			leaveType = (LeaveType) query.uniqueResult();
		
			return leaveType;
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	public Map<String, Float> getLeaveBalances(Long employeeId){
		Map<String, Float> leaveBalances = new HashMap<String, Float>();
		try{
			
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			String queryString = "from LeaveBalance lb where lb.employee.employeeId = :employeeId";
			Query query = session.createQuery(queryString).setLong("employeeId", employeeId);
			List<LeaveBalance> leaveBalancesList = (List<LeaveBalance>) query.list();
			for(LeaveBalance balance : leaveBalancesList){
				if(balance.getLeaveType().getLeaveType().equals(CommonLookup.SICKLEAVE_TYPE)){
					leaveBalances.put(CommonLookup.SICKLEAVE_TYPE, balance.getBalance());
				}else if(balance.getLeaveType().getLeaveType().equals(CommonLookup.PRIVILIZEDLEAVE_TYPE)){
					leaveBalances.put(CommonLookup.PRIVILIZEDLEAVE_TYPE, balance.getBalance());
				}else if(balance.getLeaveType().getLeaveType().equals(CommonLookup.CASUALLEAVE_TYPE)){
					leaveBalances.put(CommonLookup.CASUALLEAVE_TYPE, balance.getBalance());
				}else if(balance.getLeaveType().getLeaveType().equals(CommonLookup.OPTIONALLEAVE_TYPE)){
					leaveBalances.put(CommonLookup.OPTIONALLEAVE_TYPE, balance.getBalance());
				}else{
					leaveBalances.put(CommonLookup.WORKFROMHOME_TYPE, balance.getBalance());
				}
				
				
				
			}
			System.out.println("LEAVE  BALANCES SIZE IS:"+leaveBalances.size());
			return leaveBalances;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
		
	}
	
	
	public static void main(String a[]){
		//new LeaveTypeDAO().getLeaveTypes();
		//LeaveType leaveType = new LeaveType();
		///leaveType.setLeaveType("aaaaaaaa");
		//new LeaveTypeDAO().createLeaveType(leaveType);
		new LeaveTypeDAO().getLeaveBalances(1L);
		
	}

}
